home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 March / Macworld CD-ROM (March 1995).cdr / Updaters / AppMaker 1.5.2->1.5.4 / Libraries / THINK / AMLibraryC / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-26  |  8.8 KB  |  426 lines  |  [TEXT/KAHL]

  1. /* © 1988-91, Bowers Development Corp. */
  2. /* EventLoop.c */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Desk.h>
  8. #include <Dialogs.h>
  9. #include <DiskInit.h>
  10. #include <Editions.h>
  11. #include <EPPC.h>
  12. #include <Events.h>
  13. #include <Fonts.h>
  14. #include <GestaltEqu.h>
  15. #include <Lists.h>
  16. #include <Menus.h>
  17. #include <OSEvents.h>
  18. #include <TextEdit.h>
  19. #include <ToolUtils.h>
  20. #include <Traps.h>
  21. #include <AppleEvents.h>
  22.  
  23. #include "Globals.h"
  24. #include "Cursors.h"
  25. #include "DoScrap.h"
  26. #include "Miscellany.h"
  27. #include "WindowAids.h"
  28. #include "Windowing.h"
  29. #include "Dispatcher.h"
  30. #include "MainMenu.h"
  31. #include "AEvent.h"
  32.  
  33. #include "EventLoop.h"
  34.  
  35. /*----------*/
  36. void DoMouseDown    (void);
  37. void DoMouseUp        (void);
  38. void DoKeystroke    (void);
  39. void DoActivate        (void);
  40.  
  41. /*----------*/
  42. #if defined(applec) /* MPW C */
  43. extern void _DataInit (void);
  44. #endif /* MPW C */
  45.  
  46. /*----------*/
  47. Boolean TrapAvailable    (short            tNumber,
  48.                          TrapType        tType);
  49. Boolean TrapAvailable    (short            tNumber,
  50.                          TrapType        tType)
  51. {
  52.     return (NGetTrapAddress (tNumber, tType)
  53.             != GetTrapAddress (_Unimplemented));
  54. } /*TrapAvailable*/
  55.  
  56. #if defined(applec) /* MPW C */ || defined(THINK_C)
  57. /*----------*/
  58. static void SetStackSize    (long        stackSize);
  59. static void SetStackSize    (long        stackSize)
  60. {
  61.     SetApplLimit ((Ptr) ((long) &stackSize - stackSize));
  62. } /*SetStackSize*/
  63. #endif /* MPW C or Think C */
  64.  
  65. /*----------*/
  66. static void    GetSysConfig    (void);
  67. static void    GetSysConfig    (void)
  68. {
  69.     OSErr            ignoreError;
  70.     long            tempLong;        /*for Gestalt*/
  71.     SysEnvRec        environs;
  72.  
  73.     sysConfig.hasGestalt = (Gestalt (gestaltVersion, &tempLong) == noErr);
  74.     if (sysConfig.hasGestalt) {
  75.         sysConfig.hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
  76.         ignoreError = Gestalt (gestaltQuickdrawVersion, &tempLong);
  77.         sysConfig.hasColorQD = (tempLong != gestaltOriginalQD);
  78.         sysConfig.hasAppleEvents = (Gestalt (gestaltAppleEventsAttr, &tempLong) == noErr);
  79.         sysConfig.hasEditionMgr = (Gestalt (gestaltEditionMgrAttr, &tempLong) == noErr);
  80.         if (sysConfig.hasEditionMgr) {
  81.             if (!CheckOS (InitEditionPack ())) {
  82.                 sysConfig.hasEditionMgr = false;    /*unable to load Edition Manager package*/
  83.             }
  84.         }
  85.     } else {
  86.         ignoreError = SysEnvirons (curSysEnvVers, &environs);
  87.         if (environs.machineType < 0) {
  88.             sysConfig.hasWNE = false;
  89.         } else {
  90.             sysConfig.hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
  91.         }
  92.         sysConfig.hasColorQD = environs.hasColorQD;
  93.         sysConfig.hasAppleEvents = false;
  94.         sysConfig.hasEditionMgr = false;
  95.     }
  96. } /*GetSysConfig*/
  97.  
  98. /*----------*/
  99. void Initialize        ()
  100. {
  101.  
  102. #if defined(applec) /* MPW C */
  103.     UnloadSeg ((Ptr) _DataInit);
  104. #endif /* MPW C */
  105.  
  106. #if defined(applec) /* MPW C */ || defined(THINK_C)
  107.     SetStackSize (15000);
  108. #endif /* MPW C or Think C */
  109.  
  110.     MaxApplZone ();
  111.     MoreMasters ();
  112.     
  113.     InitGraf (&qd.thePort);
  114.     InitFonts ();
  115.     InitWindows ();
  116.     InitMenus ();
  117.     TEInit ();
  118.     InitDialogs (NULL);
  119.     
  120.     FlushEvents (everyEvent - diskMask - app4Mask, 0);
  121.  
  122.     GetSysConfig ();    
  123.     if (sysConfig.hasAppleEvents) {
  124.         InitializeAE ();
  125.     }
  126.     inBackground = false;
  127.     
  128.     LoadMenus ();
  129.     LoadCursors ();
  130.     InitScrap (); 
  131.     
  132.     InitGlobals ();
  133.  
  134.     InitModelessDialogs ();    
  135.     InitTitles ();        /*initialize each menu title's module*/
  136. } /*Initialize*/
  137.  
  138. /*----------*/
  139. void DoMouseDown    ()
  140. {
  141.     WindowPtr        whichWindow;
  142.     short            whichPart;
  143.  
  144.     whichPart = FindWindow (curEvent.where, &whichWindow);
  145.     switch (whichPart) {
  146.     case inDesk:
  147.             /*Do nothing*/;
  148.         break;
  149.     case inMenuBar:
  150.             DoMenu (MenuSelect (curEvent.where));
  151.         break;
  152.     case inSysWindow:
  153.             SystemClick (&curEvent, whichWindow);
  154.         break;
  155.     case inContent:
  156.             DoContent (whichWindow);
  157.         break;
  158.     case inDrag:
  159.             DoDrag (whichWindow);
  160.         break;
  161.     case inGrow:
  162.             DoGrow (whichWindow);
  163.         break;
  164.     case inGoAway:
  165.             DoGoAway (whichWindow);
  166.         break;
  167.     case inZoomIn:
  168.             DoZoom (whichWindow, inZoomIn);
  169.         break;
  170.     case inZoomOut:
  171.             DoZoom (whichWindow, inZoomOut);
  172.         break;
  173.     } /*switch*/
  174. } /*DoMouseDown*/
  175.  
  176. /*----------*/
  177. void DoMouseUp        ()
  178. {
  179.     WindowPtr        whichWindow;
  180.     short            whichPart;
  181.  
  182.     whichPart = FindWindow (curEvent.where, &whichWindow);
  183.     /*save event time to check later for double click*/
  184. } /*DoMouseUp*/
  185.  
  186. /*----------*/
  187. void DoKeystroke    ()
  188. {
  189.     short            charCode;
  190.     char            ch;
  191.  
  192.     charCode = curEvent.message & charCodeMask;
  193.     ch = charCode;
  194.     
  195.     if ((curEvent.modifiers & cmdKey) != 0) {
  196.         if (curEvent.what != autoKey) {
  197.             DoMenu (MenuKey (ch));
  198.         }
  199.     } else {
  200.         if (curWindow == NULL) {
  201.             SysBeep (1);
  202.         } else {
  203.             TypeInWindow (ch);
  204.         }
  205.     }
  206. } /*DoKeystroke*/
  207.  
  208. /*----------*/
  209. void DoUpdate    ()
  210. {
  211.     GrafPtr            savePort;
  212.     WindowPtr        saveWindow;
  213.     WindowPtr        whichWindow;
  214.  
  215.     GetPort (&savePort);
  216.     whichWindow = (WindowPtr) curEvent.message;
  217.     SetPort (whichWindow);
  218.     saveWindow = curWindow;
  219.     SetInfo (whichWindow);
  220.     BeginUpdate (whichWindow);
  221.         EraseRect (&(whichWindow->portRect));
  222.         UpdateContent ();
  223.         DrawControls (whichWindow);
  224.     EndUpdate (whichWindow);
  225.     SetInfo (saveWindow);    
  226.     SetPort (savePort);
  227. } /*DoUpdate*/
  228.  
  229. /*----------*/
  230. void DoActivate        ()
  231. {
  232.     Boolean            activate;
  233.     WindowPtr        whichWindow;
  234.  
  235.     whichWindow = (WindowPtr) curEvent.message;
  236.     SetPort (whichWindow);
  237.     SetInfo (whichWindow);
  238.     
  239.     activate = ((curEvent.modifiers & activeFlag) != 0);
  240.     if (activate) {
  241.         if ((cur->text) != NULL) {
  242.             TEActivate (cur->text);
  243.         }
  244.         ReadDeskScrap ();
  245.     }
  246.     
  247.     HiliteScroll (cur->vScroll, activate);
  248.     HiliteScroll (cur->hScroll, activate);
  249.  
  250.     ActivateContent (activate);
  251.  
  252.     if (!activate) {
  253.         WriteDeskScrap ();
  254.         if (cur->text != NULL) {
  255.             TEDeactivate (cur->text);
  256.         }
  257.         SetInfo (NULL);
  258.     }
  259. } /*DoActivate*/
  260.  
  261. /*----------*/
  262. void DoDiskEvent    ()
  263. {
  264.     #define dlgTop        75                                   
  265.     #define dlgLeft        100
  266.  
  267.     OSErr            ignore;
  268.     Point            where;
  269.  
  270.     if (HiWord (curEvent.message) != noErr) {
  271.         DILoad ();
  272.         SetPt (&where, dlgLeft, dlgTop); 
  273.         ignore = DIBadMount (where, curEvent.message);
  274.         DIUnload ();
  275.     }
  276. } /*DoDiskEvent*/
  277.  
  278. /*----------*/
  279. void DoApp4Event    ()
  280. {
  281.     #define suspendResumeMsg    1
  282.     #define resumeMask            1
  283.     
  284.     Boolean            resume;
  285.     WindowPtr        front;
  286.  
  287.     curEvent.what = nullEvent;
  288.     if (BitShift (curEvent.message, -24) == suspendResumeMsg) {
  289.         resume = ((curEvent.message & resumeMask) != 0);
  290.         if (resume) {
  291.             ReadDeskScrap ();
  292.         } else {
  293.             WriteDeskScrap ();
  294.         }
  295.         inBackground = !resume;
  296.         front = FrontWindow ();
  297.         if (front != NULL) {
  298.             curEvent.what = activateEvt;
  299.             curEvent.modifiers = LoWord (curEvent.message);
  300.             curEvent.message = (long) front;
  301.         }
  302.     }
  303. } /*DoApp4Event*/
  304.  
  305. /*----------*/
  306. static long GetSleep    (void);
  307. static long GetSleep    (void)
  308. {
  309.     long            sleep;
  310.  
  311.     sleep = GetCaretTime ();    /* if long time, some Inits won't run */
  312.     if ((!inBackground) && (FrontWindow () != NULL)) {
  313.         if (FrontWindow () == curWindow) {
  314.             if (cur->text != NULL) {
  315.                 if ((**(cur->text)).selStart == (**(cur->text)).selEnd) {
  316.                     sleep = GetCaretTime ();
  317.                 }
  318.             }
  319.         } else {    /* DA or modeless dialog */
  320.             sleep = GetCaretTime ();
  321.         }
  322.     }
  323.     return (sleep);
  324. } /*GetSleep*/
  325.  
  326. /*----------*/
  327. static Boolean GetEvent    (void);
  328. static Boolean GetEvent    (void)
  329. {
  330.     Boolean            gotEvent;
  331.  
  332.     if (sysConfig.hasWNE) {
  333.         gotEvent = WaitNextEvent (everyEvent, &curEvent,
  334.                                   GetSleep (), cursorRgn);
  335.     } else {
  336.         SystemTask ();
  337.         gotEvent = GetNextEvent (everyEvent, &curEvent);
  338.     }
  339.     return (gotEvent);
  340. } /*GetEvent*/
  341.  
  342. /*----------*/
  343. static Boolean DoEvent        (void);
  344. static Boolean DoEvent        (void)
  345. {
  346.     Boolean            gotEvent;
  347.     DialogPtr        whichDialog;
  348.     short            itemHit;
  349.  
  350.     gotEvent = GetEvent ();
  351.     if (curEvent.what == app4Evt) {
  352.       /* here so event won't get swallowed by IsDialogEvent */
  353.       /* and so suspend/resume can be translated to activate */
  354.         DoApp4Event ();
  355.     }
  356.     if (IsDialogEvent (&curEvent)) {
  357.         if ((curEvent.what == activateEvt)
  358.         ||  (curEvent.what == updateEvt)) {
  359.             whichDialog = (DialogPtr) curEvent.message;
  360.         } else {
  361.             whichDialog = FrontWindow ();
  362.         }
  363.         SetPort (whichDialog);
  364.         if ((Boolean) FilterModeless (whichDialog, &curEvent, &itemHit)) {
  365.             DoModelessItem (whichDialog, itemHit);
  366.         } else if ((curEvent.what == keyDown)
  367.                && ((curEvent.modifiers & cmdKey) != 0)) {
  368.             DoKeystroke ();        /* => calls MenuKey */
  369.         } else if (DialogSelect (&curEvent, &whichDialog, &itemHit)) {
  370.             DoModelessItem (whichDialog, itemHit);
  371.         }
  372.     } else if (gotEvent) {
  373.         switch (curEvent.what) {
  374.         case mouseDown:
  375.                 DoMouseDown ();
  376.             break;
  377.         case mouseUp:
  378.                 DoMouseUp ();
  379.             break;
  380.         case keyDown:
  381.         case autoKey:
  382.                 DoKeystroke ();
  383.             break;
  384.         case updateEvt:
  385.                 DoUpdate ();
  386.             break;
  387.         case activateEvt:
  388.                 DoActivate ();
  389.             break;
  390.         case diskEvt:
  391.                 DoDiskEvent ();
  392.             break;
  393.         case kHighLevelEvent:
  394.                 if (sysConfig.hasAppleEvents) {
  395.                     DoHighLevelEvent ();
  396.                 }
  397.             break;
  398.         default:
  399.                 DoIdle ();
  400.             break;
  401.         } /*switch*/
  402.     } else {        /*nullEvent*/
  403.         DoIdle ();
  404.     }
  405.     return (gotEvent);
  406. } /*DoEvent*/
  407.  
  408. /*----------*/
  409. void MainLoop    ()
  410. {
  411.     UpdateMenus ();
  412.  
  413.     quittingTime = false;    
  414.     while (!quittingTime) {
  415.         ShapeCursor ();
  416.         if (cur->text != NULL) {
  417.             TEIdle (cur->text);
  418.         }
  419.         if (DoEvent ()) {
  420.             UpdateMenus ();
  421.         }
  422.     } /*while*/
  423.     
  424.     WriteDeskScrap ();
  425. } /*MainLoop*/
  426.